package com.yaoli.ajob;

import java.util.Stack;

/**
 * Created by will on 2017/4/24.
 */
public class _150_EvaluateReversePolishNotation {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<Integer>();

        for(int i = 0 ; i < tokens.length ; i ++){
            if(tokens[i].equals("+") || tokens[i].equals("-") ||
                    tokens[i].equals("*") || tokens[i].equals("/")){
                int temp2 = stack.pop();
                int temp1 = stack.pop();

                int res =0 ;

                if(tokens[i].equals("+")){
                    res = temp1 + temp2;
                }else if(tokens[i].equals("-")){
                    res = temp1 - temp2;
                }else if(tokens[i].equals("*")){
                    res = temp1 * temp2;
                }else if(tokens[i].equals("/")){
                    res = temp1 / temp2;
                }

                stack.push(res);
            }else{
                stack.push(Integer.valueOf(tokens[i]));
            }
        }

        return stack.pop();
    }
}
